package com.example.administrator.customvp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Window;

import com.example.administrator.view.ViewPagerIndicator;
import com.example.administrator.view.VpSimpleFragment;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends FragmentActivity {

    private ViewPager mViewPager ;
    private ViewPagerIndicator mIndicator ;
    //每个fragment中的内的Textview的内容
    private List<String> mTitles = Arrays.asList("短信","收藏","推荐","短信A","收藏A");
//    private List<String> mTitles = Arrays.asList("短信","收藏","推荐","短信A","收藏A","推荐A","短信B","收藏B","推荐B");
    private List<VpSimpleFragment> mContents = new ArrayList<>() ;
    private FragmentPagerAdapter mAdapter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE) ;
        setContentView(R.layout.activity_main2);
        initView();
        initDatas();

        //如果在xml布局中没有对tab内容进行编写（动态设置tab里面的内容）
        mIndicator.setVisibleTabCount(4);
      //这里的mTitles就为tab中的内容啦
        mIndicator.setTabItemTitles(mTitles);



        mViewPager.setAdapter(mAdapter);
        mIndicator.setViewPager(mViewPager, 0);
        //在activity中使用viewpage的滑动事件
      /*  mIndicator.setOnPagechangeListener(new ViewPagerIndicator.PageOnchangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
*/
    }

    private void initView() {

        mViewPager = (ViewPager) findViewById(R.id.viewpager) ;
        mIndicator = (ViewPagerIndicator) findViewById(R.id.indicator) ;
    }

    private void initDatas() {
       for (String title:mTitles){
           VpSimpleFragment fragment = VpSimpleFragment.newInstance(title) ;
           mContents.add(fragment) ;
       }

        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mContents.get(position);
            }

            @Override
            public int getCount() {
                return mContents.size();
            }
        };

    }

}
